<%@page language="java" contentType="text/html; charset=utf-8"	pageEncoding="utf-8"%>
<html>
<head>
<script type="text/javascript">
var listUrl="rest/userList.do";
var userStatusJson;         
 
$(document).ready(function () {

	userStatusJson=syncGetDictListByTypeName('status');

    $("#submit-search").click(function () {
        $('#user-dg').datagrid({ queryParams: form2Json("fms") });   //点击搜索
    });


    $("#user-dg").datagrid({
        loadMsg: '数据加载中....',
        //title: '用户管理',
        //height:document.body.clientHeight-60,
        width: '100%',
        url: listUrl,        
// 		loadFilter : function(result) {
// 				if (result.retCode == '0000') {
// 					return result.data;
// 				} else {
// 					return result.retMsg;
// 				}
// 			},
			nowrap : false,
			striped : true,
			remoteSort : false,
			rownumbers : true,
			pagination : true,
			fitColumns : true,
			pageList : [ 10, 15, 20, 25 ],
			frozenColumns : [ [ {
				field : 'id',
				checkbox : true
			}, {
				title : '用户编号',
				field : 'id',
				width : 0,
				hidden : 'true'
			} ] ],
			columns : [ [ {
				field : 'adminPhone',
				title : '手机号',
				width : 100
			},
			 {
				field : 'adminName',
				title : '姓名',
				width : 100
			},
			 {
				field : 'identificationCard',
				title : '身份证',
				width : 100
			},
			{
				field : 'status',
				title : '状态',
				width : 100,
				formatter : function(val) {
					return formatDictValue(val, userStatusJson);
				}
			},
			{
				field : 'opt',
				title : '操作',
				width : 100,
				align : 'center',
				formatter: function(value,row,index){
					if(row.status == 1){
						return "<a href='javascript:stateChange("+row.id+",0);'>禁用</a>";
					}else if(row.status==0){
						return "<a href='javascript:stateChange("+row.id+",1);'>启用</a>";;
					}
				}
			}

			] ]
		});// end datagrid
		//设置分页控件
		var p = $('#user-dg').datagrid('getPager');
		$(p).pagination({
			pageSize : 10,//每页显示的记录条数，默认为10
			pageList : [ 10, 15, 20, 25 ],//可以设置每页记录条数的列表
			beforePageText : '第',//页数文本框前显示的汉字
			afterPageText : '页    共 {pages} 页',
			displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录',
			onBeforeRefresh : function() {
				$(this).pagination('loading');
				$(this).pagination('loaded');
			}

		});//end pagination

	})
	//end ready
	
	function stateChange(id,status){
		$.post("rest/userUpdate.do",{"id":id,"status":status},function(data){
			if(data.retCode=="0000"){
				$('#user-dg').datagrid("reload");
			}
		});
		
	}
	function newUser() {
		var url = "user/add.do";
		$.post(url, {}, function(data) {
			$("#center").empty();
			$("#center").html(data);
			$.parser.parse($("#center"));
		});
	}

	function editUser() {
		var rows = $('#user-dg').datagrid('getSelections');
		if (rows.length != 1) {
			$.messager.alert('操作提示', "请先选择一条要编辑的信息！");
			return;
		}
		var row = $('#user-dg').datagrid('getSelected');
		if (row.username == 'admin') {
			$.messager.alert('操作提示', "admin用户不可编辑！");
			return;
		}
		var id = row.id;

		var url = "user/edit.do";
		$.post(url, row, function(data) {
			$("#center").empty();
			$("#center").html(data);
			$.parser.parse($("#center"));
		});
	}
	
	function detailUser() {
		var rows = $('#user-dg').datagrid('getSelections');
		if (rows.length != 1) {
			$.messager.alert('操作提示', "请先选择一条要编辑的信息！");
			return;
		}
		var row = $('#user-dg').datagrid('getSelected');
		var id = row.id;
		var url = "user/detail.do";
		$.post(url, row, function(data) {
			$("#center").empty();
			$("#center").html(data);
			$.parser.parse($("#center"));
		});
	}

	function removeUser() {
		var array = $('#user-dg').datagrid('getSelections');
		var ids = new Array();

		for ( var i = 0; i < array.length; i++) {//组成一个字符串，ID主键之间用逗号隔开
			if (array[i].username == 'admin') {
				$.messager.alert('操作提示', "不能删除admin用户");
				return;
			}
			ids[i] = array[i].id;
		}

		ids = ids.join(',');

		if (array != "") {
			$.messager.defaults = {
				ok : "确定",
				cancel : "取消"
			};
			$.messager.confirm('操作提示', '是否要删除该信息？', function(r) {
				if (r) {
					$.post('rest/userDel.do', {
						ids : ids
					}, function(response) {

						if (response.retCode == "0000") {
							$('#user-dg').datagrid('reload');
							$.messager.alert('操作提示', "删除成功", 'info');							
						} else {
							$.messager.alert('操作提示', "删除失败", 'error');
						}
					});
				}
			});
		} else {
			$.messager.alert('操作提示', "请先选择要删除的信息！");
		}

	};

	function editRole() {
		var rows = $('#user-dg').datagrid('getSelections');
		if (rows.length != 1) {
			$.messager.alert('操作提示', "请先选择一个用户的信息！");
			return;
		}
		var row = $('#user-dg').datagrid('getSelected');
		if (row.username == 'admin') {
			$.messager.alert('操作提示', "admin用户不可分配角色！");
			return;
		}
		var url = "userrole/edituserrole.do";
		$.post(url, ("userId=" + row.id), function(data) {
			$("#center").empty();
			$("#center").html(data);
			$.parser.parse($("#center"));
		});
	}
</script>
</head>
<body>



<!-- 		<div class="breadcrumb">系统 &gt;&gt; 用户管理</div> -->
		<!-- <div class="breadcrumb">系统 &gt;&gt; 用户管理</div> -->
		<div class="linkbar"><a>系统</a>/<a>用户管理</a></div>
		<div class="formtoolbar">
		<form id="fms" method="post" novalidate>
			    <i>手机号</i> 
				<input name="adminPhone"  class="easyui-validatebox" />
				<a id="submit-search" class="easyui-linkbutton">查询</a>						
			</form>
		</div>
	</div> 
		<div class="clear"></div>
		<div id="user-dg" toolbar="#toolbar"></div>

		<div id="toolbar">
			<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true"	onclick="newUser()">添加用户</a>
			<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">编辑用户</a>
			<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="detailUser()">详情</a>
			<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeUser()">删除用户</a> 
 			<a href="#"	class="easyui-linkbutton" iconCls="icon-edit" plain="true"	onclick="editRole()">分配角色</a>
		</div>
</body>
</html>